<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\LoginModel;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    //登录页面
    public function index()
    {
        if (auth()->check()){
            return view('admin.index.index');
        }
        return view('admin.login.login');
    }

    //登录处理
    public function login(Request $request){
        if ($request->isMethod('post')){

            $demo = $this->validate($request,[
               'username' => 'required',
               'password' => 'required'
            ],[
                'username.required' => '账号不能为空',
                'password.required' => '密码不能为空'
            ]);

            $data = auth()->attempt($demo);

            if ($data){
                return redirect(route('admin.index'));
            }
            return redirect(route('admin.login'))->withErrors(['error'=>'账号或密码错误']);
        }
    }

    //退出登录
    public function logout(){
        return auth()->logout();
        return redirect(route('admin.login'))->withErrors(['error'=>'您已退出登录']);
    }
}
